CLASS zcl_moon_i_api012 DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_rap_query_provider.
  PROTECTED SECTION.
  PRIVATE SECTION.
    TYPES:
        tt_tb TYPE STANDARD TABLE OF ymoont_api012.

    METHODS call_out_api EXPORTING out_tab TYPE ymoonst_api012.

    METHODS get_data_from_request
      IMPORTING io_request TYPE REF TO if_rap_query_request
      EXPORTING rt_result  TYPE tt_tb
      RETURNING VALUE(r_count) type i
      RAISING   cx_rap_query_filter_no_range.
ENDCLASS.



CLASS ZCL_MOON_I_API012 IMPLEMENTATION.


  METHOD call_out_api.
    DATA:http_client TYPE REF TO ymoon_api.
    CREATE OBJECT http_client
      EXPORTING
        i_scenario     = 'YMOON_CS_API012'
        i_service      = 'YMOON_OS_API012_REST'
      EXCEPTIONS
        no_arrangement = 1.
    IF sy-subrc = 1.
      "out->write( 'no arrangement' ).
    ENDIF.
    CHECK sy-subrc <> 1.

    DATA param TYPE string.
    DATA res TYPE string.
    param = 'crkyCn=u270f148l018m219a020m040c0&qryYymmDd=20150101&imexTp=2'.

    res = http_client->get( param ).


    DATA: lv_xstring TYPE xstring.
    DATA: lv_conv TYPE REF TO cl_abap_conv_codepage.
    lv_xstring = cl_abap_conv_codepage=>create_out( codepage = `UTF-8` )->convert( source = res ).
    "out->write( lv_xstring ).

    DATA:BEGIN OF ls_data,
           cntysgn   TYPE c  LENGTH 2,
           mtryutnm  TYPE c  LENGTH 30,
           fxrt      TYPE c  LENGTH 13,
           currsgn   TYPE c  LENGTH 3,
           aplybgndt TYPE c  LENGTH 8,
           imextp    TYPE c  LENGTH 1,
         END OF ls_data.
    DATA:lt_data LIKE TABLE OF ls_data.

    DATA:gv_cond TYPE c LENGTH 50 VALUE 'trifFxrtInfoQryRsltVo'.
    DATA:gv_field TYPE c LENGTH 20.
    FIELD-SYMBOLS <fs> TYPE any.


    DATA: lv_reader          TYPE REF TO if_sxml_reader.
    DATA: lv_node            TYPE REF TO if_sxml_node.
    DATA: lv_open_node       TYPE REF TO if_sxml_open_element.
    DATA: lv_close_node      TYPE REF TO if_sxml_close_element.
    DATA: lv_value_node      TYPE REF TO if_sxml_value.
    DATA: lv_value           TYPE string.

    lv_reader = cl_sxml_string_reader=>create( lv_xstring ).

    lv_node = lv_reader->read_next_node( ).


    ASSERT lv_node IS NOT INITIAL.
    WHILE lv_reader->node_type <> if_sxml_node=>co_nt_final.
      CASE lv_reader->node_type.
        WHEN if_sxml_node=>co_nt_element_open.
          lv_open_node  ?= lv_node.

          gv_field = 'ls_data-' && lv_open_node->qname-name.

        WHEN if_sxml_node=>co_nt_element_close.
          lv_close_node ?= lv_node.

          IF lv_close_node->qname-name = gv_cond.
            APPEND ls_data TO lt_data.
            CLEAR ls_data.
          ELSE.
            ASSIGN (gv_field) TO <fs>.
            IF sy-subrc = 0.
              <fs> = lv_value.
            ENDIF.
          ENDIF.

        WHEN if_sxml_node=>co_nt_value.
          lv_value_node ?= lv_node.
          lv_value = lv_value_node->get_value( ).
        WHEN if_sxml_node=>co_nt_attribute.
      ENDCASE.
      lv_node = lv_reader->read_next_node( ).
    ENDWHILE.

    MOVE-CORRESPONDING lt_data TO out_tab .



  ENDMETHOD.


  METHOD get_data_from_request.
    TYPES:
      tt_r_cntysgn   TYPE RANGE OF ymoont_api012-cntysgn,
      tt_r_mtryutlnm TYPE RANGE OF ymoont_api012-mtryutlnm.

    DATA lt_r_key  TYPE tt_r_cntysgn.
    DATA lt_r_text TYPE tt_r_mtryutlnm.


    DATA(lt_filter) = io_request->get_filter( )->get_as_ranges( ).
    DATA(ld_offset) = io_request->get_paging( )->get_offset( ).
    DATA(ld_pagesize) = io_request->get_paging( )->get_page_size( ).

    LOOP AT lt_filter INTO DATA(ls_filter).
      CASE ls_filter-name.
        WHEN 'Cntysgn'.
          lt_r_key = CORRESPONDING #( ls_filter-range ).
        WHEN 'Mtryutlnm'.
          lt_r_text = CORRESPONDING #( ls_filter-range ).

      ENDCASE.
    ENDLOOP.

    SELECT * FROM ymoont_api012
          WHERE cntysgn IN @lt_r_key
            AND mtryutlnm IN @lt_r_text
          ORDER BY cntysgn
          INTO TABLE @rt_result
          UP TO @ld_pagesize ROWS
          OFFSET @ld_offset .


    select count( * ) from ymoont_api012 into @data(lv_count).
    r_count = lv_count.

  ENDMETHOD.


  METHOD if_rap_query_provider~select.
    DATA: lt_dbtab TYPE STANDARD TABLE OF ymoont_api012,
          ls_dbtab TYPE ymoont_api012.
    DATA: lt_out TYPE ymoonst_api012.

    DATA: lt_database TYPE STANDARD TABLE OF ymoont_api012.

    DATA lt_output TYPE STANDARD TABLE OF ymoon_i_api012.
    DATA ls_output TYPE ymoon_i_api012.
    DATA(lo_system_uuid) = NEW cl_system_uuid( ).


    call_out_api( IMPORTING out_tab = lt_out ).

    LOOP AT lt_out INTO DATA(ls_out).
      CLEAR ls_dbtab.
      MOVE-CORRESPONDING ls_out TO ls_dbtab.
      TRY.
          ls_dbtab-uuid = lo_system_uuid->if_system_uuid~create_uuid_x16( ).
        CATCH cx_uuid_error.
      ENDTRY.

      ls_dbtab-reg_id = sy-uname.
      ls_dbtab-reg_date = sy-datum.
      APPEND ls_dbtab TO lt_dbtab.


    ENDLOOP.

    DELETE FROM ymoont_api012.
    MODIFY ymoont_api012 FROM TABLE @lt_dbtab.


    data lv_count type int8.
    lv_count = get_data_from_request( EXPORTING io_request = io_request IMPORTING rt_result = lt_database ).
*
    CLEAR lt_output.
    LOOP AT lt_database INTO DATA(ls_line).
      CLEAR ls_output.
      MOVE-CORRESPONDING ls_line TO ls_output.
      ls_output-regid = ls_line-reg_id.
      ls_output-regdate = ls_line-reg_date.
      APPEND ls_output TO lt_output.
    ENDLOOP.



    IF io_request->is_data_requested( ).
      io_response->set_data( lt_output ).
    ENDIF.

    IF io_request->is_total_numb_of_rec_requested( ).
      io_response->set_total_number_of_records( lv_count ).
    ENDIF.
  ENDMETHOD.
ENDCLASS.
